package cn.sharetraffic.mi.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.sharetraffic.mi.PushApiManager;


public class TestServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1468290939903122541L;
	private StringBuilder sb = new StringBuilder();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		String content = req.getParameter("content");
		if (content == null || content.equals("")) {
			content = "默认值";
		}
		final java.io.Writer out = resp.getWriter();
		PushApiManager manager = PushApiManager.getInstance();
		String submitType = req.getParameter("form_type");
		sb.setLength(0);
		if(submitType == null){
			sb.append("form_type参数不正确");
		}
		else if(submitType.equals("1")) {
			//JPush推送
			PushResult result = manager.pushMessageToAll(Message.content(content));
			sb.append("JPush推送【");
			sb.append(content);
			sb.append("】--> ");
			if (result.isResultOK()) {
				sb.append("成功");
			} else {
				sb.append("失败");
			}
		}
		else if(submitType.equals("2")){
			//短信推送
			manager.sendBySms(content);
			sb.append("短信推送【");
			sb.append(content);
			sb.append("】到本机 ");
		}
		out.write(sb.toString());
		out.flush();
	}

}
